package org.sl.flvplayer.elements {
	import org.sl.flvplayer.controls.PlayerLogic;
	import org.sl.flvplayer.elements.ControllerElement;
	import org.sl.flvplayer.flyweights.FWPlayerCommand;

	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	/**
	 * Provides controls for the Volumebar.
	 * 
	 * @package org.sl.flvplayer.elements
	 * @author Dan
	 * @version
	 * @see org.sl.flvplayer.controls.IControllerElement
	 * @see org.sl.flvplayer.controls.Controller
	 */ 
	public class VolumeControllerElement extends ControllerElement {
		
		private const MAXVOLUME : Number = 100;
		
		private var volumeButton_mc : MovieClip;
		private var volumeBar_mc : MovieClip;
		private var currentVol : Number;
		private var currentPos : Number;

		/**
		 * VolumeControllerElement consturctor removes the higlightcolor listeners and enables mouseinteraction for the subelements of the btnVolume element.
		 * 
		 * @param p_allowScale
		 */
		public function VolumeControllerElement(p_allowScale : Boolean = false) {
			super();
			allowScale = p_allowScale;	
			mouseChildren = true;
			removeEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
			removeEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
		}

		/**
		 */
		override public function addSkin(p_skin : MovieClip, p_type : String) : void {
			var obj : Object = {skin:p_skin, type:p_type};
			skinList.push(obj);
		}

		/**
		 */
		override public function showSkin(p_type : String) : void {
			if (aktSkin) removeChild(aktSkin);
			for (var i:String in skinList) {
				if (skinList[i].type == p_type) {
					aktSkin = addChild(skinList[i].skin);
				}
			}
			initMovieClips();		
		}	

		/**
		 * Initializes all the parts of the volumebar which are used for user interaction.
		 */
		private function initMovieClips() : void {			
			volumeBar_mc = MovieClip(aktSkin).getChildByName("volumeBar") as MovieClip;
			volumeBar_mc.addEventListener(MouseEvent.CLICK, onMouseClick);
			
			volumeButton_mc = MovieClip(aktSkin).getChildByName("volumeButton") as MovieClip;
			volumeButton_mc.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown);	
		}	 

		/**
		 * Mousedown handler.
		 * 
		 * @param p_event
		 */
		private function mouseDown(p_event : MouseEvent) : void {			
			stage.addEventListener(MouseEvent.MOUSE_UP, mouseUp);	
			var seekrec : Rectangle = new Rectangle((volumeBar_mc.width / 2) + volumeBar_mc.x, volumeBar_mc.y + (volumeButton_mc.height / 2), 0, volumeBar_mc.height - (volumeButton_mc.height / 2));
			volumeButton_mc.startDrag(false, seekrec);
		}

		/**
		 * Mouseup handler.
		 * 
		 * @param p_event
		 */
		private function mouseUp(p_event : MouseEvent) : void {		
			volumeButton_mc.stopDrag();	
			seekData();	
			stage.removeEventListener(MouseEvent.MOUSE_UP, mouseUp);
		}

		/**
		 * Mouseclick handler.
		 * 
		 * @param p_event 
		 */
		private function onMouseClick(p_event : MouseEvent) : void {
			seekData();		
		}

		/**
		 * Updates the volume and the position of the volumeButton_mc.
		 * 
		 * @param vol volume
		 */
		public function volUpdate(vol : Number) : void {			
			currentPos = volumeBar_mc.height + volumeBar_mc.y - ((vol / MAXVOLUME) * volumeBar_mc.height);	
			if(currentPos <= volumeBar_mc.y) {
				currentPos = volumeBar_mc.y + (volumeButton_mc.height / 2);
			}
			if(currentPos >= volumeBar_mc.height) {
				currentPos = volumeBar_mc.height + (volumeButton_mc.height / 2);
			}
			volumeButton_mc.y = currentPos;
		}

		/**
		 * Calculates the volume value and sets the VOLUME command.
		 */
		private function seekData() : void {
			if(volumeBar_mc.mouseY <= 3) { 
				currentVol = MAXVOLUME;
			}		
			if(volumeBar_mc.mouseY >= volumeBar_mc.height - 3) {	
				currentVol = 0;
				PlayerLogic.getInstance().command(new FWPlayerCommand(FWPlayerCommand.MUTE, this));
			} else {
				currentVol = MAXVOLUME - volumeBar_mc.mouseY * (MAXVOLUME / volumeBar_mc.height);
			}		
			PlayerLogic.getInstance().command(new FWPlayerCommand(FWPlayerCommand.VOLUME, this, [currentVol]));
			volUpdate(currentVol);
		}
	}
}
